import pandas as pd
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
from pyecharts.faker import Faker


class BarChart_flow_workday_weekend_timeline():
    
    def __init__(self, dataset, place=0):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1
        
        #主程序入口
        self.main()
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘制条形图函数
        self.paintBarChart()
        
    
    #获取数据
    def getData(self):
            
        df=self.paint_data.loc[:,['date','flow']] #取出数据集中的日期和客流数据
        # df=df[self.year] #筛选某一年的天气数据
        df.columns = ['date','flow'] #添加表头名称

        df['date'] = pd.to_datetime(df['date']) #将数据类型转换为日期类型
        self.paint_data = df.set_index('date') # 将date设置为index  
      
    
    def paintBarChart(self):
        
        if self.place==0:
            place='九寨沟'
            years=range(2013, 2017) #设置年份范围
            fileName="3.3 九寨沟2013-2016年每月工作日与双休日平均客流数条形图（含时间轴）.html"
        else:
            place='四姑娘山'
            years=range(2016, 2020) #设置年份范围
            fileName="3.3 四姑娘山2016-2019年每月工作日与双休日平均客流数条形图（含时间轴）.html"
        
        tl = Timeline()
        for year in years:
            
            flow_workday_sums=[0,0,0,0,0,0,0,0,0,0,0,0,0]
            flow_weekend_sums=[0,0,0,0,0,0,0,0,0,0,0,0,0]
            
            flow_workday_avgs=[0,0,0,0,0,0,0,0,0,0,0,0,0]
            flow_weekend_avgs=[0,0,0,0,0,0,0,0,0,0,0,0,0]
            
            month_workday_nums = [0,0,0,0,0,0,0,0,0,0,0,0,0]
            month_weekend_nums = [0,0,0,0,0,0,0,0,0,0,0,0,0]            
            
            for i in range(1,13):
                daynums = 0
                
                format=str(year)+'-{}'
                time=format.format(i)
                # print(time)
                for day in self.paint_data[time].index:
                    # print(day)
                    daynums+=1
                    if day.weekday()>0 and day.weekday()<6:
                        flow_workday_sums[i]+=int(self.paint_data[format.format(str(i)+'-'+str(daynums)):format.format(str(i)+'-'+str(daynums))].values)
                        #print(int(df['2018-{}'.format(str(i)+'-'+str(daynums)):'2018-{}'.format(str(i)+'-'+str(daynums))].values))
                        month_workday_nums[i]+=1
                        
                    else:
                        flow_weekend_sums[i] +=int(self.paint_data[format.format(str(i)+'-'+str(daynums)):format.format(str(i)+'-'+str(daynums))].values)
                        month_weekend_nums[i]+=1
                        
                flow_workday_avgs[i]=flow_workday_sums[i]/month_workday_nums[i]
                flow_weekend_avgs[i]=flow_weekend_sums[i]/month_weekend_nums[i]
            
            #处理平均值为int值
            for i in range(1,13):
                flow_workday_avgs[i] = int( flow_workday_avgs[i])
                flow_weekend_avgs[i] = int( flow_weekend_avgs[i])
            
            flow_workday_avgs=flow_workday_avgs[1:13]
            flow_weekend_avgs=flow_weekend_avgs[1:13]
            # # 将工作日和周六日平均客流合并为一个DataFrame 
            # flow_avg = pd.DataFrame({'工作日':flow_workday_avgs[1:13],'双休日':flow_weekend_avgs[1:13]})
            
            # flow_avg.columns = ['工作日','双休日'] #设置表头名称
            labels = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'] #设置dataFrame的索引
            # flow_avg.index=index #设置索引
         
            c = (
                Bar(init_opts=opts.InitOpts(width="900px", height="600px"))
                .add_xaxis(labels)
                .add_yaxis("工作日", flow_workday_avgs)
                .add_yaxis("双休日", flow_weekend_avgs)
                .set_global_opts(
                    title_opts=opts.TitleOpts(title=place+" {} 年每月工作日与双休日平均客流量".format(year)),
                    toolbox_opts=opts.ToolboxOpts(),
                    legend_opts=opts.LegendOpts(is_show=False),
                )
            )            
            tl.add(c, "{}年".format(year))
        tl.render('resultFile/'+fileName)
        
        print(fileName+"已生成！")

